package com.hadluo.qq.server.mapper;

import com.hadluo.qq.server.entity.GroupMessage;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface GroupMessageMapper {

    @Insert("INSERT INTO group_messages (sender_id, group_id, type, content, file_path, file_name, file_size, status, created_at, updated_at) " +
            "VALUES (#{senderId}, #{groupId}, #{type}, #{content}, #{filePath}, #{fileName}, #{fileSize}, #{status}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(GroupMessage message);

    @Select("SELECT * FROM group_messages WHERE id = #{id}")
    GroupMessage selectById(Long id);

    @Select("SELECT * FROM group_messages WHERE group_id = #{groupId} ORDER BY created_at ASC LIMIT #{limit} OFFSET #{offset}")
    List<GroupMessage> selectByGroupId(@Param("groupId") Long groupId, @Param("offset") int offset, @Param("limit") int limit);

    @Update("UPDATE group_messages SET status = #{status}, updated_at = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);

    @Delete("DELETE FROM group_messages WHERE id = #{id}")
    int deleteById(Long id);

    @Select("SELECT COUNT(*) FROM group_messages WHERE group_id = #{groupId}")
    int countByGroupId(Long groupId);

    @Select("SELECT * FROM group_messages WHERE group_id = #{groupId} AND created_at > #{timestamp} ORDER BY created_at ASC")
    List<GroupMessage> selectNewMessages(@Param("groupId") Long groupId, @Param("timestamp") java.util.Date timestamp);

    @Select("SELECT * FROM group_messages WHERE group_id = #{groupId} ORDER BY id DESC LIMIT 1")
    GroupMessage selectLastMessageByGroupId(Long groupId);
}